﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Exam.BLL;
using Exam.Model;
using Exam.DBHelper;
using System.Data.SqlClient;
using ExaminationWeb.Common;
namespace ExaminationWeb.Controllers
{
    public class ContentTypeController : Controller
    {
        //
        // GET: /ContentType/
        ModalityService modalityService = new ModalityService();
        ProductService productService = new ProductService();
        ContentTypeService contentTypeService = new ContentTypeService();

        public ActionResult TypeList()
        {
            ViewData["ModalityList"] = modalityService.LoadEntites(e => e.IsActive == 0);
            return View();
        }
        #region
        [AllowAnonymous]
        public ActionResult TypeAdd()
        {
            ViewData["ModalityList"] = modalityService.LoadEntites(e => e.IsActive == 0);
            return View();
        }
        public ActionResult TypeUpdate(string id)
        {
            ViewData["ModalityList"] = modalityService.LoadEntites(e => e.IsActive == 0);

            ContentType _ctype = contentTypeService.LoadEntites(e => e.TypeID == Guid.Parse(id)).FirstOrDefault();
            ViewData["ContentType"] = _ctype;
            if (_ctype != null)
            {
                ViewData["ProductList"] = productService.LoadEntites(e => e.ModalityID == _ctype.ModalityID);
            }
            return View();
        }
        [AllowAnonymous]
        public JsonResult GetAllInfos()
        {
            int pageIndex = Request["sEcho"] == null ? 1 : int.Parse(Request["sEcho"]);
            int pageSize = Request["iDisplayLength"] == null ? 15 : int.Parse(Request["iDisplayLength"]);
            int pageStart = Request["iDisplayStart"] == null ? 0 : int.Parse(Request["iDisplayStart"]);

            string SortDirindex = Request["iSortCol_0"] == null ? "" : Request["iSortCol_0"];
            string SortDir = "";
            string DataProp = "";
            if (!string.IsNullOrEmpty(SortDirindex))
            {
                DataProp = Request["mDataProp_" + SortDirindex] == null ? "" : Request["mDataProp_" + SortDirindex];
                SortDir = Request["sSortDir_0"] == null ? "" : Request["sSortDir_0"]; //asc or desc
            }
            int totalcount = 0;
            string Modality = Request["Modality"] == null ? "" : Request["Modality"];
            string Product = Request["Product"] == null ? "" : Request["Product"];
            string TypeName = Request["TypeName"] == null ? "" : Request["TypeName"];

            string IsActive = Request["isactive"] == null ? "-1" : Request["isactive"];

            List<Exam.Model.ExtentionModels.ExContentType> listType = new Exam.BLL.ExtendBLL.ContentType().GetTypeList(Modality, Product, TypeName, IsActive, DataProp, SortDir == "asc", pageStart, pageSize, out totalcount);
            var result = new { iTotalDisplayRecords = totalcount, iTotalRecords = totalcount, aaData = listType, sEcho = 0 };
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public string Updateinfo(ContentType contentType)
        {
            try
            {
                contentType = new ContentType();
                contentType.TypeID = Guid.Parse(Request.Form["TypeID"]);
                contentType.ModalityID = Guid.Parse(Request.Form["ModalityID"]);
                contentType.ProductID = Guid.Parse(Request.Form["ProductID"]);
                contentType.TypeName = Request["ContentType"];
                contentType.IsActive = int.Parse(Request["IsActive"]);
                contentType.CreateTime = DateTime.Parse(Request.Form["CreateTime"]);
                contentType.CreateUserID = Guid.Parse(Request.Form["CreateUserID"]);
                ContentType _type = IsExistData(contentType);

                if (_type.TypeID.ToString() == "00000000-0000-0000-0000-000000000000" || _type.TypeID == contentType.TypeID)
                {
                    contentType.UpdateTime = DateTime.Now;
                    contentType.UpdateUserID = UserAccount.UserID;
                    contentTypeService.UpdateEntity(contentType);
                    return "suc";
                }
                else
                {
                    return "repeat";
                }
            }
            catch (Exception e)
            {
                return "err";
            }
        }
        #endregion
        [HttpPost]
        public string AddInfo(ContentType contentType)
        {
            try
            {
                contentType = new ContentType();
                contentType.ModalityID = Guid.Parse(Request["ModalityID"]);
                contentType.ProductID = Guid.Parse(Request["ProductID"]);
                contentType.TypeName = Request["ContentType"];
                contentType.IsActive = int.Parse(Request["IsActive"]);

                if (IsExistData(contentType).TypeID.ToString() == "00000000-0000-0000-0000-000000000000")
                {
                    contentType.TypeID = Guid.NewGuid();
                    DateTime time = DateTime.Now;
                    contentType.CreateTime = time;
                    contentType.CreateUserID = UserAccount.UserID;
                    contentType.UpdateTime = time;
                    ContentType newProvince = contentTypeService.AddEntity(contentType);

                    return "suc";
                }
                else
                {
                    return "repeat";
                }
            }
            catch (Exception e)
            {
                return "";
            }
        }

        #region 公用
        public ContentType IsExistData(ContentType contentType)
        {
            SQLHelper commonservice = new SQLHelper();
            Dictionary<string, SqlParameter> dic = new Dictionary<string, SqlParameter>();

            SqlParameter parname = new SqlParameter("@TypeName", System.Data.SqlDbType.NVarChar);
            parname.Value = contentType.TypeName;
            dic.Add("TypeName", parname);
            return commonservice.getExistData<ContentType>("ContentType", dic);
        }

        #endregion
    }
}
